package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.R.id;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.objects.clsDictionary;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class Lanzamientos extends Activity {

	public static ArrayList<Dictionary> Actividades = null;
	private Dictionary dic;
	public ListView list;
	public Dialog progressDialog;
	public App_ImageLoader imageLoader;
	private Intent intent;
	public static ArrayList<Dictionary<String,String>> TraeDatos = null;
	public static ArrayList<Dictionary<String,String>> Splash = null;
	public Integer EVEIDR;
	
	public void voidFinish(View V) {
		finish();
	}
	
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Checks the orientation of the screen
	}
	
	public void abrirMenuLateral(View V) {
		intent = new Intent(Lanzamientos.this, MenuLateral.class);
		startActivity(intent);
	}

	@Override
	public void onBackPressed() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_lanzamientos);

		TextView Titulo = (TextView) this.findViewById(id.tv_listView_Titulo);
		Titulo.setText(getString(R.string.st_lanzamientos));
		EVEIDR = Utilities.EVE_IDR;

		//splash
		try{
			String qry = "select a.* from eventos_banners a inner join banners_catalogo b on a.ban_idr = b.ban_idr where a.eve_idr = "+EVEIDR+" and b.ban_nombre = 'SPLASH LANZAMIENTOS' ORDER BY b.ban_nombre";
			Splash = Utilities.qry2(getApplicationContext(), qry);
			
	    	if(Splash.size()>0){
	    		Dictionary<String, String> dic = Splash.get(0);
		    	String strPoster = dic.get("ban_url").toString();
			    String strTimer = dic.get("ban_timer").toString();
			    String strPatrocinador = dic.get("pat_idr").toString();
			    if (strPoster.equals("null")) {
			        strPoster = "";
			    }
			    if(strPoster.length()>0){
					intent = new Intent(this, Splash2.class);
					Bundle bund = new Bundle();
					bund.putString("PUT_IMG",strPoster);
					bund.putString("PUT_PAT",strPatrocinador);
					bund.putString("PUT_TIME",strTimer);
					intent.putExtras(bund);
					startActivity(intent);
			    }
		    }
		}catch(Exception ex){}
	 
		CargarContenido();
	}

	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncContenido().execute("");
	}
	
	private class AsyncContenido extends AsyncTask<String, String, String>{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			progressDialog = ProgressDialog.show(Lanzamientos.this, "","Cargando...");

		}

		@Override
		protected String doInBackground(String... params) {
			
			String qry ="select * from lanzamientos where eve_idr=" + EVEIDR ;
			
			TraeDatos = Utilities.qry2(getApplicationContext(),qry);
			return null;
		}
		
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			
			Resources res = getResources();
			
			ListView listView = (ListView) findViewById(id.list);
			LanzamientosArrayAdapter adapter = new LanzamientosArrayAdapter(Lanzamientos.this, TraeDatos, res.getString(R.string.QRY_SERVLET));
			listView.setAdapter(adapter);
				
			
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
					muestra_detalle(TraeDatos.get(position).get("lan_idr").toString());
				}
			});
			progressDialog.dismiss();
			
		}
		
	}

	private void  muestra_detalle(String idlan){
		
		intent = new Intent(this, LanzamientosDetalle.class);
		Bundle bund = new Bundle();
		bund.putString("idlanzamiento",idlan);
		intent.putExtras(bund);
		startActivity(intent);
		
	}
	
}
